package com.zy.pigchat_server.aspect;

import com.zy.pigchat_server.utlis.AuthUtls;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.TypeVariable;

/**
 * @author 这是那头猪？
 * @version 1.0
 * @description: 权限切面
 * @date 2023/7/26 11:23
 */
@Slf4j
@Order(1)
@Aspect
@Component
public class AuthAspect {
    @Before("execution(* com.zy.pigchat_server.controller.*.*(..))")
    public void authBeFor(JoinPoint joinPoint){
        log.info("权限校验增强....");
        //获取目标对象
        Object target = joinPoint.getTarget();
        //获取目标方法名
        String methodName = joinPoint.getSignature().getName();
        //获取目标方法参数的类型的数组
        TypeVariable[] typeParameters = joinPoint.getSignature().getDeclaringType().getTypeParameters();
        //进行权限校验，没权限会抛出异常
        AuthUtls.checkAuth(target, methodName, typeParameters);
        log.info("权限校验成功.....");
    }
}
